package com.dbh.company.chaos;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Map;

/**
 * 触发yongGC
 * @author dongbanghui
 * @create 2023/6/19
 */
public class YongGC {

    public Object run(Map<String, Object> params) throws InterruptedException {
        Logger logger = LoggerFactory.getLogger(YongGC.class);
        logger.info("注入gc攻击开始...");
        // 循环创建对象并返回
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < 1000 * 30) {
            for (int i = 0; i < 10000; i++) {
                yongGC();
            }
        }
        Thread.sleep(1000 * 3);
        long startTime2 = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime2 < 1000 * 30) {
            for (int i = 0; i < 10000; i++) {
                yongGC();
            }
        }
        logger.info("注入gc攻击结束...");
        return null;
    }
    public User yongGC() {
        User user = new User();
        user.setId(1);
        user.setName("张");
        return user;
    }

    public static class User implements Serializable {

        private static final long serialVersionUID = 1L;

        /**
         * 主键
         */
        private Integer id;

        /**
         * 名称
         */
        private String name;

        /**
         * 年龄
         */
        private Integer age;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }
}
